package com.itheima.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.User;
import com.itheima.domain.VO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/test")
public class ControllerTest {
    @RequestMapping("/suc1")
    public String test1(){
        System.out.println("fdsfsdf");
        return "success";
    }

    @RequestMapping("/suc2")
    public ModelAndView test2(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username","ff");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/suc3")
    public ModelAndView test3(ModelAndView modelAndView){
        modelAndView.addObject("username","绣眼鸟");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/suc4")
    public String test4(Model model){
        model.addAttribute("username","博学谷");
        return "success";
    }

    @RequestMapping("/suc5")
    public String test5(Model model){
        model.addAttribute("username","博学谷");
        return "success";
    }

    @RequestMapping("/suc6")
    public String test6(HttpServletRequest request){
        request.setAttribute("username","长颈鹿");
        return "success";
    }


    @RequestMapping("/suc7")
    @ResponseBody
    public String test7(){
       return "fffffff";
    }

    @RequestMapping("/suc8")
    @ResponseBody
    public String test8(){
        return "{'username':'zhangsan','age':'99'}";
    }

    @RequestMapping("/suc9")

    public void test9(HttpServletResponse response) throws IOException {
        response.getWriter().print("hell");
    }

    @RequestMapping("/suc10")
    @ResponseBody
    public String test10() throws JsonProcessingException {

        User user = new User();
        user.setUsername("wo");
        user.setAge(55);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);

        return json;
    }

    @RequestMapping("/suc11")
    @ResponseBody
    public User test11() throws JsonProcessingException {

        User user = new User();
        user.setUsername("woff");
        user.setAge(55);

        return user;
    }


    @RequestMapping("/suc12")
    @ResponseBody
    public void test12(String username,int age) throws JsonProcessingException {
        System.out.println(username);
        System.out.println(age);
    }

    @RequestMapping("/suc13")
    @ResponseBody
    public void test13(User user) throws JsonProcessingException {
        System.out.println(user);
    }

    @RequestMapping("/suc14")
    @ResponseBody
    public void test14(String[] str)  {
        System.out.println(Arrays.asList(str));
    }

    @RequestMapping("/suc15")
    @ResponseBody
    public void test15(VO vo)  {
        System.out.println(vo);
    }

    @RequestMapping("/suc16")
    @ResponseBody
    public void test16(@RequestBody List<User> userList) throws IOException {
        System.out.println(userList);
    }

    @RequestMapping("/suc17")
    @ResponseBody
    public void test17(@RequestParam(value="name",required = false,defaultValue = "itcast") String username) throws IOException {
        System.out.println(username);
    }


    @RequestMapping("/suc18/{name}")
    @ResponseBody
    public void test18(@PathVariable(value="name") String username) throws IOException {
        System.out.println(username);
    }

    @RequestMapping("/suc19")
    @ResponseBody
    public void test19(Date date) throws IOException {
        System.out.println(date);
    }

    @RequestMapping("/suc20")
    @ResponseBody
    public void test20(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);
    }

    @RequestMapping("/scu21")
    @ResponseBody
    public void test21(@RequestHeader(value = "User-Agent",required = false) String user_agent) throws IOException {
        System.out.println(user_agent);
    }

    @RequestMapping("/suc22")
    @ResponseBody
    public void test22(@CookieValue(value = "JSESSIONID") String jsessionId) throws IOException {
        System.out.println(jsessionId);
    }

    @RequestMapping("/suc23")
    @ResponseBody
    public void test23(String username, MultipartFile uploadFile) throws IOException {
        System.out.println(username);
        String originalFilename = uploadFile.getOriginalFilename();
        uploadFile.transferTo(new File("D:\\upload\\"+originalFilename));
    }

    @RequestMapping("/suc24")
    @ResponseBody
    public void test24(String username, MultipartFile[] uploadFile) throws IOException {
        System.out.println(username);
        for (MultipartFile multipartFile : uploadFile) {
            String originalFilename = multipartFile.getOriginalFilename();
            multipartFile.transferTo(new File("D:\\upload\\"+originalFilename));
        }
    }
}
